package com.egao.common.module.cooperative.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.Constants;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.cooperative.constants.CooperativeProjectConstants;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivity;
import com.egao.common.module.cooperative.entity.CooperativeProjectActivityStudent;
import com.egao.common.module.cooperative.entity.CooperativeProjectRecord;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectActivityListVO;
import com.egao.common.module.cooperative.entity.vo.CooperativeProjectStudentBtn;
import com.egao.common.module.cooperative.mapper.CooperativeProjectActivityMapper;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityService;
import com.egao.common.module.cooperative.service.CooperativeProjectActivityStudentService;
import com.egao.common.module.cooperative.service.CooperativeProjectRecordService;
import com.egao.common.module.cooperative.service.CooperativeProjectService;
import com.egao.common.module.student.entity.Student;
import com.egao.common.module.student.service.StudentService;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 产教融合项目活动服务接口实现
 *
 * @author root
 */
@Service
public class CooperativeProjectActivityServiceImpl
        extends ServiceImpl<CooperativeProjectActivityMapper, CooperativeProjectActivity> implements CooperativeProjectActivityService {

    @Autowired
    private CooperativeProjectRecordService cooperativeProjectRecordService;
    @Autowired
    private CooperativeProjectService cooperativeProjectService;
    @Autowired
    private CooperativeProjectActivityStudentService cooperativeProjectActivityStudentService;
    @Autowired
    private StudentService studentService;

    @Override
    public List<CooperativeProjectActivityListVO> listPage(PageParam<CooperativeProjectActivity> page) {
        return baseMapper.listPage(page);
    }

    @Override
    public List<CooperativeProjectActivityListVO> listPageActivity(PageParam<CooperativeProjectActivity> page) {
        page.put("type", CooperativeProjectConstants.CPA_TYPE_ACTIVITY);
        page.setSize(9999);
        return listPage(page);
    }

    @Override
    public List<CooperativeProjectActivityListVO> listPageShine(PageParam<CooperativeProjectActivity> page) {
        page.put("type", CooperativeProjectConstants.CPA_TYPE_SHINE);
        return listPage(page);
    }

    @Override
    public List<CooperativeProjectActivityListVO> listPageDynamic(PageParam<CooperativeProjectActivity> page) {
        page.put("type", CooperativeProjectConstants.CPA_TYPE_DYNAMIC);
        return listPage(page);
    }

    @Override
    public List<CooperativeProjectActivityListVO> listPagePublicity(PageParam<CooperativeProjectActivity> page) {
        page.put("state", CooperativeProjectConstants.CPA_STATE_SUCCESS);
        page.put("releaseState", CooperativeProjectConstants.CPA_RELEASE_YES);
        return baseMapper.listPagePublicity(page);
    }

    @Override
    public JsonResult addCooperativeProjectActivity(CooperativeProjectActivity cooperativeProjectActivity, String role) {
        cooperativeProjectActivity.setState(CooperativeProjectConstants.CPA_STATE_SUBMIT);
        cooperativeProjectActivity.setReleaseState(CooperativeProjectConstants.CPA_RELEASE_STATE);
        if (baseMapper.insert(cooperativeProjectActivity) > 0) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProjectActivity.getCooperativeProjectId(),
                    cooperativeProjectActivity.getId(),
                    role,
                    CooperativeProjectConstants.operateInsert(cooperativeProjectActivity.getType())));
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Override
    public JsonResult updateCooperativeProjectActivity(CooperativeProjectActivity cooperativeProjectActivity, String role) {
        if (CooperativeProjectConstants.CPA_STATE_FAIL.equals(cooperativeProjectActivity.getState())) {
            cooperativeProjectActivity.setState(CooperativeProjectConstants.CPA_STATE_SUBMIT);
        }
        cooperativeProjectActivity.setReleaseState(CooperativeProjectConstants.CPA_STATE_SUBMIT);
        if (baseMapper.updateById(cooperativeProjectActivity) > 0) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProjectActivity.getCooperativeProjectId(),
                    cooperativeProjectActivity.getId(),
                    role,
                    CooperativeProjectConstants.operateUpdate(cooperativeProjectActivity.getType())));
            return JsonResult.ok("保存成功");
        }
        return JsonResult.error("保存失败");
    }

    @Override
    public JsonResult deleteCooperativeProjectActivity(Integer id, String role) {
        CooperativeProjectActivity cooperativeProjectActivity = getById(id);

        UpdateWrapper<CooperativeProjectActivity> uw = new UpdateWrapper<>();
        uw.lambda()
                .set(CooperativeProjectActivity::getDeleted, Constants.DELETED_FALSE)
                .eq(CooperativeProjectActivity::getId, id);
        if (update(uw)) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProjectActivity.getCooperativeProjectId(),
                    id,
                    role,
                    CooperativeProjectConstants.operateDelete(cooperativeProjectActivity.getType())));
            return JsonResult.ok("删除成功");
        }

        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult detail(Integer id, String role) {
        CooperativeProjectActivity cooperativeProjectActivity = getById(id);


        cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProjectActivity.getCooperativeProjectId(),
                id,
                role,
                CooperativeProjectConstants.operateView(cooperativeProjectActivity.getType())));

        return JsonResult.ok().setData(cooperativeProjectActivity);
    }

    @Override
    public JsonResult audit(CooperativeProjectActivity cooperativeProjectActivity) {
        CooperativeProjectActivity old = getById(cooperativeProjectActivity);
        if (old == null) {
            throw new BusinessException("参数异常");
        }
        String content = "";
        if (CooperativeProjectConstants.CPA_RELEASE_STATE.equals(old.getReleaseState())) {
            content = "活动审核";
        } else {
            content = "活动发布审核";
        }

        // 不是活动类型并且审核通过时，赋值活动发布状态也是通过
        if (!CooperativeProjectConstants.CPA_TYPE_ACTIVITY.equals(cooperativeProjectActivity.getType())
        && CooperativeProjectConstants.CPA_STATE_SUCCESS.equals(cooperativeProjectActivity.getState())) {
            cooperativeProjectActivity.setReleaseState(CooperativeProjectConstants.CPA_STATE_SUCCESS);
        }

        if (updateById(cooperativeProjectActivity)) {
            cooperativeProjectRecordService.add(new CooperativeProjectRecord(cooperativeProjectActivity.getCooperativeProjectId(),
                    cooperativeProjectActivity.getId(),
                    CooperativeProjectConstants.RR_SCHOOL,
                    CooperativeProjectConstants.operateAudit(cooperativeProjectActivity.getType(), cooperativeProjectActivity.getState()),
                    content));

            return JsonResult.ok("审核成功");
        }
        return JsonResult.error("审核失败");
    }

    @Override
    public CooperativeProjectStudentBtn initBtn(CooperativeProjectActivity cooperativeProjectActivity, User user) {
        if (user == null) {
            return new CooperativeProjectStudentBtn("请先登录");
        }

        if (!UserConstants.USER_TYPE_STUDENT.equals(user.getUserType())) {
            return new CooperativeProjectStudentBtn("仅限学生报名");
        }

        Student student = studentService.getByUserId(user.getUserId());
        if (student == null) {
            return new CooperativeProjectStudentBtn("账号信息异常请联系就业中心");
        }

        CooperativeProjectActivityStudent cooperativeProjectActivityStudent = cooperativeProjectActivityStudentService.getById(cooperativeProjectActivity.getId(), student.getId());
        if (cooperativeProjectActivityStudent != null) {
            switch (cooperativeProjectActivityStudent.getState()) {
                case CooperativeProjectConstants.CPAS_STATE_APPLY:
                    if (System.currentTimeMillis() > cooperativeProjectActivity.getCloseTime().getTime()) {
                        return new CooperativeProjectStudentBtn("已报名");
                    } else {
                        return new CooperativeProjectStudentBtn("取消报名", false, cooperativeProjectActivityStudent.getId());
                    }
                case CooperativeProjectConstants.CPAS_STATE_CANCEL:
                    return new CooperativeProjectStudentBtn("已取消报名");
                default:
            }
        }

        if (System.currentTimeMillis() > cooperativeProjectActivity.getCloseTime().getTime()) {
            return new CooperativeProjectStudentBtn("报名已截止");
        }

        Integer countApply = cooperativeProjectActivityStudentService.countApply(cooperativeProjectActivity.getId());
        if (countApply >= cooperativeProjectActivity.getNumber()) {
            return new CooperativeProjectStudentBtn("报名名额已满");
        }

        return new CooperativeProjectStudentBtn("报名", false);
    }
}
